const mongoose = require("mongoose")
const md5=require("blueimp-md5")
mongoose.connect("mongodb://localhost:27017/quest", { useNewUrlParser: true, useUnifiedTopology: true })

const conn = mongoose.connection

conn.on('connected', function () {
    console.log('database connect successfully')
})

const userSchema = mongoose.Schema({
    username: {type: String, required: true},
    password: {type: String, required: true},
    type: {type: String, required: true},
})

const UserModel =mongoose.model('user',userSchema)
var a = "yes"
function testSave() {
    const userModel= new UserModel({username: a,password: md5("yys"),type:"laoban"})

    userModel.save(function (error,user) {
        console.log("save()",error,user)
    })
}

function testfind() {
    UserModel.find(function (error,us) {
        console.log('find()',error,us)
    })

    UserModel.findOne({username:'qianzhentao',type:'laoban'},function (error,user) {
        console.log('findOne()',error,user)
    })
}

function testUpdate() {
    UserModel.findByIdAndUpdate({username:'qianzhentao'},{type:'xiaojj'},function (error,doc) {
        console.log('findByid',error,doc)
    })
}

function testremove() {
    UserModel.remove({username:"qianzhentao"},function (error,doc) {
        console.log("remove",error,doc)
    })
}
testSave()
// testUpdate()
// testfind()
// testremove()